package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.ymatou.iapi.synctrade.parameter.mongo.Order;
import com.ymatou.iapi.trading.sellerquery2.parameter.GetDeclareOrdersByIdsReq;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.GetDeclareOrdersResp;
import com.ymatou.iapi.trading.sellerquery2.service.GetDeclareOrdersByIdsCall;
import com.ymatou.query.seller.trading.api.parameter.info.OrderBasicInfo;
import com.ymatou.query.seller.trading.api.parameter.info.OrderConsigneeInfo;
import com.ymatou.query.seller.trading.api.parameter.info.OrderConsumerInfo;
import com.ymatou.query.seller.trading.api.parameter.info.OrderPaymentDetail;
import com.ymatou.query.seller.trading.api.parameter.info.OrderPaymentInfo;
import com.ymatou.query.seller.trading.api.parameter.info.OrderProductInfo;
import com.ymatou.query.seller.trading.api.parameter.info.OrderSellerInfo;
import com.ymatou.query.seller.trading.api.parameter.info.OrderTransferInfo;
import com.ymatou.query.seller.trading.api.parameter.info.SellerOrderInfo;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.common.order.sellerquery2.SellerOrderInfoBuilder;
import com.ymttest.common.order.sellerquery2.model.FundSummary;
import com.ymttest.common.order.userquery.util.MongoHelper;
import com.ymttest.common.order.userquery.util.OrderHelper;
import com.ymttest.database.mongo.OrderQueryWapper;
import com.ymttest.database.mongo.config.OrderField;
import com.ymttest.utils.DataUtil;

public class Ts_GetDeclareOrdersByIdsCall {

	private static GetDeclareOrdersByIdsReq getDeclareOrdersByIdsReq;
	private static GetDeclareOrdersByIdsCall getDeclareOrdersByIdsCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据订单id查询申报订单");
	}

	@Before
	public void caseUp() {
		getDeclareOrdersByIdsReq = new GetDeclareOrdersByIdsReq();
		getDeclareOrdersByIdsCall = new GetDeclareOrdersByIdsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getDeclareOrdersByIdsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() throws Exception {
		GetDeclareOrdersResp expectResponse = new GetDeclareOrdersResp();
		GetDeclareOrdersResp actualResponse = getDeclareOrdersByIdsCall
				.getDeclareOrdersResp();

		ConstructExpectedResponse(expectResponse);

		List<SellerOrderInfo> expects = expectResponse.getOrders();
		List<SellerOrderInfo> actuals = actualResponse.getOrders();
		int i = 0;

		for (SellerOrderInfo actual : actuals) {

			verifyBasic(expects.get(i).getBasic(), actual.getBasic());
			verifyBuyer(expects.get(i).getBuyer(), actual.getBuyer());
			verifySeller(expects.get(i).getSeller(), actual.getSeller());
			verifyConsignee(expects.get(i).getConsignee(),
					actual.getConsignee());
			verifyProducts(expects.get(i).getProducts(),
					actual.getProducts());
			this.verifyPayment(expects.get(i).getPayment(),
					actual.getPayment());
			this.verifyTransfer(expects.get(i).getTransfer(),
					actual.getTransfer());

			i = i + 1;
		}
	}

	private void ConstructExpectedResponse(GetDeclareOrdersResp response)
			throws Exception {

		// 根据orderId获取order部分字段及退款信息
		Query query = new Query();
		OrderQueryWapper orderQueryWapper = new OrderQueryWapper();
		query.addCriteria(Criteria.where(OrderField.ID).in(
				getDeclareOrdersByIdsReq.getOrderIds()));

		MongoHelper.excludeField(query.fields(), OrderField.STATUS_LOG,
				OrderField.DELIVERY);

		List<Order> orderList = orderQueryWapper.find(query);
		OrderHelper.hiddenOrder(orderList);
		List<SellerOrderInfo> sellerOrderInfos = SellerOrderInfoBuilder
				.convertBySellerOrder(orderList,
						getDeclareOrdersByIdsReq.getSellerId());
		response.setOrders(sellerOrderInfos);
	}

	private void verifyBasic(OrderBasicInfo expect, OrderBasicInfo actual) {

		Logger.comment("开始验证basic:" + expect.getId());

		Logger.verifyEquals(expect.getId(), actual.getId(), "验证id");
		Logger.verifyEquals(expect.getMainOrderId(), actual.getMainOrderId(),
				"验证mainOrderId");
		Logger.verifyEquals(expect.getCreatedTime(), actual.getCreatedTime(),
				"验证createdTime");
		Logger.verifyEquals(expect.getAcceptedTime(), actual.getAcceptedTime(),
				"验证acceptedTime");
		Logger.verifyEquals(expect.getSource(), actual.getSource(), "验证source");
		Logger.verifyEquals(expect.getType(), actual.getType(), "验证type");
		Logger.verifyEquals(expect.getStatus(), actual.getStatus(), "验证status");
		DataUtil.verifyDecimalEquals(expect.getTotalPrice(),
				actual.getTotalPrice(), "验证totalPrice");
		DataUtil.verifyDecimalEquals(expect.getDiscount(),
				actual.getDiscount(), "验证discount");
		Logger.verifyEquals(expect.isPreSaleOrder(), actual.isPreSaleOrder(),
				"验证preSaleOrder");
	}

	private void verifyBuyer(OrderConsumerInfo expect, OrderConsumerInfo actual) {

		Logger.comment("开始验证buyer");

		Logger.verifyEquals(expect.getId(), actual.getId(), "验证id");
		Logger.verifyEquals(expect.getMessage(), actual.getMessage(),
				"验证message");
	}

	private void verifySeller(OrderSellerInfo expect, OrderSellerInfo actual) {

		Logger.comment("开始验证seller");

		Logger.verifyEquals(expect.getId(), actual.getId(), "验证id");
		Logger.verifyEquals(expect.getComment(), actual.getComment(),
				"验证comment");
		Logger.verifyEquals(expect.getLoginId(), actual.getLoginId(),
				"验证loginId");
	}

	private void verifyConsignee(OrderConsigneeInfo expect,
			OrderConsigneeInfo actual) {

		Logger.comment("开始验证consignee");

		Logger.verifyEquals(expect.getName(), actual.getName(), "验证name");
		Logger.verifyEquals(expect.getAddress(), actual.getAddress(),
				"验证address");
		Logger.verifyEquals(expect.getPostcode(), actual.getPostcode(),
				"验证postcode");
	}

	private void verifyProducts(List<OrderProductInfo> expectList,
			List<OrderProductInfo> actualList) {

		Logger.comment("开始验证products");

		int i = 0;

		for (OrderProductInfo actual : actualList) {

			Logger.verifyEquals(expectList.get(i).getName(), actual.getName(),
					"验证name");

			for (int j = 0; j < actual.getCategories().size(); j++)
				Logger.verifyEquals(expectList.get(i).getCategories().get(j),
						actual.getCategories().get(j), "验证categories");

			Logger.verifyEquals(expectList.get(i).getSku(), actual.getSku(),
					"验证sku");
			DataUtil.verifyDecimalEquals(expectList.get(i).getPrice(),
					actual.getPrice(), "验证price");
			Logger.verifyEquals(expectList.get(i).getCount(),
					actual.getCount(), "验证count");
			DataUtil.verifyDecimalEquals(expectList.get(i).getTotalPrice(),
					actual.getTotalPrice(), "验证totalPrice");
			Logger.verifyEquals(expectList.get(i).getPropertyInfo(),
					actual.getPropertyInfo(), "验证propertyInfo");
			Logger.verifyEquals(expectList.get(i).getBondedArea(),
					actual.getBondedArea(), "验证bondedArea");
			Logger.verifyEquals(expectList.get(i).getLogistics(),
					actual.getLogistics(), "验证logistics");
			DataUtil.verifyDecimalEquals(expectList.get(i).getOriginalPrice(),
					actual.getOriginalPrice(), "验证originalPrice");
			DataUtil.verifyDecimalEquals(expectList.get(i).getPartialFreight(),
					actual.getPartialFreight(), "验证partialFreight");
			Logger.verifyEquals(expectList.get(i).isFreightFree(),
					actual.isFreightFree(), "验证freightFree");
			DataUtil.verifyDecimalEquals(expectList.get(i)
					.getPartialYmtCoupon(), actual.getPartialYmtCoupon(),
					"验证partialYmtCoupon");
			DataUtil.verifyDecimalEquals(expectList.get(i)
					.getPartialSellerCoupon(), actual.getPartialSellerCoupon(),
					"验证partialSellerCoupon");
			DataUtil.verifyDecimalEquals(
					expectList.get(i).getPartialDiscount(),
					actual.getPartialDiscount(), "验证partialDiscount");
			DataUtil.verifyDecimalEquals(expectList.get(i)
					.getPartialSellerPromotion(), actual
					.getPartialSellerPromotion(), "验证partialSellerPromotion");
			DataUtil.verifyDecimalEquals(expectList.get(i)
					.getPartialYmtPromotion(), actual.getPartialYmtPromotion(),
					"验证partialYmtPromotion");
			Logger.verifyEquals(expectList.get(i).getBrandId(),
					actual.getBrandId(), "验证brandId");
			Logger.verifyEquals(expectList.get(i).getCatalogId(),
					actual.getCatalogId(), "验证catalogId");
			Logger.verifyEquals(expectList.get(i).getProductId(),
					actual.getProductId(), "验证productId");
			Logger.verifyEquals(expectList.get(i).getCatalogType(),
					actual.getCatalogType(), "验证catalogType");
			Logger.verifyEquals(expectList.get(i).getActivityId(),
					actual.getActivityId(), "验证activityId");
			Logger.verifyEquals(expectList.get(i).isPreSale(),
					actual.isPreSale(), "验证preSale");
			DataUtil.verifyDecimalEquals(expectList.get(i)
					.getThirdPartyDiscount(), actual.getThirdPartyDiscount(),
					"验证thirdPartyDiscount");
			Logger.verifyEquals(expectList.get(i).isPspProduct(),
					actual.isPspProduct(), "验证pspProduct");
			DataUtil.verifyDecimalEquals(expectList.get(i).getEarnestAmount(),
					actual.getEarnestAmount(), "验证earnestAmount");
			DataUtil.verifyDecimalEquals(expectList.get(i)
					.getBookingPromotionAmount(), actual
					.getBookingPromotionAmount(), "验证bookingPromotionAmount");

			i = i + 1;
		}
	}

	private void verifyPayment(OrderPaymentInfo expect, OrderPaymentInfo actual) {

		Logger.comment("开始验证payment");

		Logger.verifyEquals(expect.getTradingId(), actual.getTradingId(),
				"验证tradingId");
		DataUtil.verifyDecimalEquals(expect.getActualPaid(),
				actual.getActualPaid(), "验证actualPaid");

		List<OrderPaymentDetail> expectDetails = expect.getDetails();
		FundSummary expectSummary = expect.getSummary();
		FundSummary actualSummary = actual.getSummary();
		int i = 0;

		for (OrderPaymentDetail actualDetails : actual.getDetails()) {

			Logger.verifyEquals(expectDetails.get(i).getType(),
					actualDetails.getType(), "验证type");
			Logger.verifyEquals(expectDetails.get(i).getPaidTime(),
					actualDetails.getPaidTime(), "验证paidTime");
			Logger.verifyEquals(expectDetails.get(i).getInternalTradingNo(),
					actualDetails.getInternalTradingNo(), "验证internalTradingNo");
			Logger.verifyEquals(expectDetails.get(i).getExternalTradingNo(),
					actualDetails.getExternalTradingNo(), "验证externalTradingNo");

			i = i + 1;
		}

		DataUtil.verifyDecimalEquals(expectSummary.getPaidCash(),
				actualSummary.getPaidCash(), "验证paidCash");
		DataUtil.verifyDecimalEquals(expectSummary.getPaidGift(),
				actualSummary.getPaidGift(), "验证paidGift");
		DataUtil.verifyDecimalEquals(expectSummary.getPaidFreeCard(),
				actualSummary.getPaidFreeCard(), "验证paidFreeCard");
		DataUtil.verifyDecimalEquals(expectSummary.getPaidYmtCoupon(),
				actualSummary.getPaidYmtCoupon(), "验证paidYmtCoupon");
		DataUtil.verifyDecimalEquals(expectSummary.getPaidSellerCoupon(),
				actualSummary.getPaidSellerCoupon(), "验证paidSellerCoupon");
		DataUtil.verifyDecimalEquals(expectSummary.getPostPaidCash(),
				actualSummary.getPostPaidCash(), "验证postPaidCash");
		DataUtil.verifyDecimalEquals(expectSummary.getPostPaidGift(),
				actualSummary.getPostPaidGift(), "验证postPaidGift");
		DataUtil.verifyDecimalEquals(expectSummary.getTotalProductPrice(),
				actualSummary.getTotalProductPrice(), "验证totalProductPrice");
		DataUtil.verifyDecimalEquals(expectSummary.getFirstPayProductPrice(),
				actualSummary.getFirstPayProductPrice(),
				"验证firstPayProductPrice");
		DataUtil.verifyDecimalEquals(expectSummary.getFreight(),
				actualSummary.getFreight(), "验证freight");
		DataUtil.verifyDecimalEquals(expectSummary.getOrderDiscount(),
				actualSummary.getOrderDiscount(), "验证orderDiscount");
		DataUtil.verifyDecimalEquals(expectSummary.getPostPayDiscount(),
				actualSummary.getPostPayDiscount(), "验证postPayDiscount");
		DataUtil.verifyDecimalEquals(expectSummary.getPaidThirdParty(),
				actualSummary.getPaidThirdParty(), "验证paidThirdParty");
		DataUtil.verifyDecimalEquals(expectSummary.getPaidAccount(),
				actualSummary.getPaidAccount(), "验证paidAccount");
		DataUtil.verifyDecimalEquals(expectSummary.getPrePaidCard(),
				actualSummary.getPrePaidCard(), "验证prePaidCard");
		DataUtil.verifyDecimalEquals(expectSummary.getTotalRefundedCash(),
				actualSummary.getTotalRefundedCash(), "验证totalRefundedCash");
		DataUtil.verifyDecimalEquals(expectSummary.getTotalRefundedGift(),
				actualSummary.getTotalRefundedGift(), "验证totalRefundedGift");
		DataUtil.verifyDecimalEquals(expectSummary.getTotalRefundedYmtCoupon(),
				actualSummary.getTotalRefundedYmtCoupon(),
				"验证totalRefundedYmtCoupon");
		DataUtil.verifyDecimalEquals(expectSummary.getTotalActivityPrice(),
				actualSummary.getTotalActivityPrice(), "验证totalActivityPrice");
		DataUtil.verifyDecimalEquals(
				expectSummary.getTotalRefundedThirdPartyDiscount(),
				actualSummary.getTotalRefundedThirdPartyDiscount(),
				"验证totalRefundedThirdPartyDiscount");
		DataUtil.verifyDecimalEquals(
				expectSummary.getTotalThirdPartyDiscount(),
				actualSummary.getTotalThirdPartyDiscount(),
				"验证totalThirdPartyDiscount");
		DataUtil.verifyDecimalEquals(
				expectSummary.getTotalRefundedYmtPromotion(),
				actualSummary.getTotalRefundedYmtPromotion(),
				"验证totalRefundedYmtPromotion");
		DataUtil.verifyDecimalEquals(expectSummary.getYmtPromotionAmount(),
				actualSummary.getYmtPromotionAmount(), "验证ymtPromotionAmount");
	}

	private void verifyTransfer(OrderTransferInfo expect,
			OrderTransferInfo actual) {

		Logger.comment("开始验证transfer");

		Logger.verifyEquals(expect.getDomesticDelivery(),
				actual.getDomesticDelivery(), "验证domesticDelivery");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetDeclareOrders_001() {
		Logger.start(true, "获取货头的申报订单");
		try {

			long SellerId = 500002490;

			getDeclareOrdersByIdsReq.setSellerId(SellerId);
			getDeclareOrdersByIdsReq.setOrderIds(Arrays.asList(4243398699L));

			getDeclareOrdersByIdsCall.setData(getDeclareOrdersByIdsReq);
			getDeclareOrdersByIdsCall.callService();

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetDeclareOrders_002() {
		Logger.start(true, "获取店主申报订单");
		try {

			long SellerId = 500032356;

			getDeclareOrdersByIdsReq.setSellerId(SellerId);
			getDeclareOrdersByIdsReq.setOrderIds(Arrays.asList(4243398699L));

			getDeclareOrdersByIdsCall.setData(getDeclareOrdersByIdsReq);
			getDeclareOrdersByIdsCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetDeclareOrders_003() {
		Logger.start(true, "获取直销商申报订单数量");
		try {

			long SellerId = 500031201;

			getDeclareOrdersByIdsReq.setSellerId(SellerId);
			getDeclareOrdersByIdsReq.setOrderIds(Arrays.asList(500031201L));

			getDeclareOrdersByIdsCall.setData(getDeclareOrdersByIdsReq);
			getDeclareOrdersByIdsCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetDeclareOrders_005() {
		Logger.start(true, "不输入sellerId");
		try {

			// long SellerId = 500031201;
			//
			// getDeclareOrdersReq.setSellerId(SellerId);
			getDeclareOrdersByIdsReq.setOrderIds(Arrays.asList(500031201L));

			getDeclareOrdersByIdsCall.setData(getDeclareOrdersByIdsReq);
			getDeclareOrdersByIdsCall.callService();

			Logger.verifyEquals(400, getDeclareOrdersByIdsCall.getCode(), "验证code");
			Logger.verifyEquals(100, getDeclareOrdersByIdsCall.getBCode(), "验证bcode");
			Logger.verifyEquals("sellerId:卖家ID不能等于0",
					getDeclareOrdersByIdsCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetDeclareOrders_006() {
		Logger.start(true, "订单id数量不正确");
		try {

			long SellerId = 500031201;
			
			getDeclareOrdersByIdsReq.setSellerId(SellerId);
			//getDeclareOrdersByIdsReq.setOrderIds(Arrays.asList(500031201L));

			getDeclareOrdersByIdsCall.setData(getDeclareOrdersByIdsReq);
			getDeclareOrdersByIdsCall.callService();

			Logger.verifyEquals(400, getDeclareOrdersByIdsCall.getCode(), "验证code");
			Logger.verifyEquals(100, getDeclareOrdersByIdsCall.getBCode(), "验证bcode");
			Logger.verifyEquals("orderIds:订单ID数量在1-200之间",
					getDeclareOrdersByIdsCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
